var app = getApp(),
  $ = require("../../utils/httpHelper.js")
Page({
  data: {
    hasAddress:false,
    addr:'',
    good_info:'',
    good_num:'',
    money:0,
    z_money:0,
    good_id:'',
    liuyan:'',
    pay_cash: false,
    carlist:[],
    fromcar:false
  },
  
  onLoad: function (options) {
    var that = this;
    console.log(options)
    var rd_session = wx.getStorageSync('rd_session');
    //来自地址
    if (!app.check_empty(options.from)&&options.from == 'addr'){
      let params = {
        rd_session: app.globalData.userInfo.rd_session,
        address_id: options.addr_id
      }
      $.httpPost("get_address_info", params, (data, err) => {
        this.setData({
          addr: data,
          hasAddress: true
        })
      })
      if (!app.check_empty(options.status) && options.status == 'car'){
        //购物车
        $.httpPost("cart_list", { rd_session: rd_session, status: 1 }, (data, err) => {
          this.setData({
            carlist: data.data.list,
            money: data.data.total_fee,
            z_money: data.data.total_fee,
            fromcar: true
          })
        })
      } else if (!app.check_empty(options.status) && options.status == 'obl'){
        //订单
        $.httpPost("pay_info", { good_id: options.good_id, num: options.num, rd_session: rd_session }, (data, err) => {
          this.setData({
            good_info: data.data,
            good_num: options.num,
            money: data.data.sell_price * options.num,
            good_id: options.good_id,
            z_money: data.data.sell_price * options.num
          })
        })
      }
    }else{
      //来自待付款
      this.getAddr()
      if (!app.check_empty(options.from) && options.from == 'car'){
        $.httpPost("cart_list", { rd_session: rd_session ,status:1}, (data, err) => {
          this.setData({
            carlist: data.data.list,
            money: data.data.total_fee,
            z_money: data.data.total_fee,
            fromcar: true
          })
        })
      }else{
        $.httpPost("pay_info", { good_id: options.good_id, num: options.num, rd_session: rd_session }, (data, err) => {
          this.setData({
            good_info: data.data,
            good_num: options.num,
            money: data.data.sell_price * options.num,
            good_id: options.good_id,
            z_money: data.data.sell_price * options.num
          })
        })
      }
    }
    
  },
  //获取地址
  getAddr(){
    let params = {
      rd_session: app.globalData.userInfo.rd_session,
    }
    $.httpPost("address_list", params, (data, err) => {
      if (data.lists.length > 0) {
        let arrs = data.lists
        let addrarr = []
        let adr = {}
        for(let addr of arrs){
          if(addr.is_default == 'y'){
            addrarr.push(addr)
          }
        }
        if (addrarr.length == 0){
          adr = arrs[0]
        }else{
          adr = addrarr[0]
        }
        this.setData({
          addr: adr,
          hasAddress: true
        })
      } else {
        this.setData({
          hasAddress: false
        })
      }
    })
  },
  message(e) {
    this.setData({
      liuyan: e.detail.value
    })
  },
  tonewAddress(){
    if (!this.data.fromcar) {
      let das = 'good_id=' + this.data.good_id + '&num=' + this.data.good_num
      wx.redirectTo({
        url: '/pages/address/address?from=obl&status=obl&' + das,
      })
    } else {
      wx.redirectTo({
        url: '/pages/address/address?from=obl&status=car',
      })
    }

    // let das = 'good_id=' + this.data.good_id + '&num=' + this.data.good_num
    // wx.redirectTo({
    //   url: '/pages/address/address?from=obl&'+das, 
    // })
  },
  tochooseaddr(){
    if (!this.data.fromcar){
      let das = 'good_id=' + this.data.good_id + '&num=' + this.data.good_num
      wx.redirectTo({
        url: '/pages/address/addressli?from=obl&status=obl&' + das,
      })
    }else{
      wx.redirectTo({
        url: '/pages/address/addressli?from=car&status=car',
      })
    }
  },
  
  
  topay() {
    if (this.data.pay_cash) {
      this.submit()
    } else {
      //余额
      if (app.check_empty(this.data.password)) {
        wx.showToast({
          title: '请输入密码',
          duration: 2000
        })
      } else {
        this.submit()
      }
    }
  },

  submit() {
    if (!this.data.hasAddress){
      wx.showToast({
        title: '请先选择地址~',
      })
      return
    }
    if(this.data.fromcar){
      //来自购物车的订单
      let params = {
        message: this.data.liuyan,
        rd_session: app.globalData.userInfo.rd_session,
      }
      $.httpPost("submit_cart_order", params, (data, err) => {
        //去支付
        if (data.result == 'success') {
          app.wxPayGood({
            order_no: data.order_no, openid: app.globalData.userInfo.openId,
            success: function () {
              wx.showToast({
                title: '支付成功哈',
              })
              wx.switchTab({
                url: '/pages/mine/mine',
              })
            },
            fail: function () {
              wx.showToast({
                title: '支付失败了呢',
              })
            }
          })
        }
      })
    }else{
      let params = {
        message: this.data.liuyan,
        rd_session: app.globalData.userInfo.rd_session,
        total_num: this.data.good_num,
        good_id: this.data.good_id,
        address_id: this.data.addr.id
      }
      $.httpPost("submit_order", params, (data, err) => {
        if (data.result == 'success') {
          app.wxPayGood({
            order_no: data.order_no, openid: app.globalData.userInfo.openId,
            success: function (res) {
              wx.showToast({
                title: '支付成功哈',
              })
              app.wxPayGoodok(res)
              wx.switchTab({
                url: '/pages/mine/mine',
              })
            },
            fail: function () {
              wx.showToast({
                title: '支付失败了呢',
              })
            }
          })
        }
      })
    }
    
  }
    
})  